package com.yicong.ants.manager.http;

import com.yicong.ants.manager.config.ZzConfigs;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * retrofit 网络请求helper
 * Created by cchao on 2017.
 *
 * @author cchao
 */
public class RetrofitHelper {

    private static OkHttpClient mOkHttpClient;
    private static Apis mApiService;
    private static Apis mApiLine;
    private static Apis mVideo;
    private static Apis mApiHotel;

    static {
        mOkHttpClient = OkHttpManager.getHttpClient();
        mApiService = initApiService(ZzConfigs.mGlobalApi, Apis.class);
        mApiHotel = initApiService(ZzConfigs.mApiHotel, Apis.class);
        mApiLine = initApiService(ZzConfigs.mApiLine, Apis.class);
//        mVideo = initApiService(ZzConfigs.mVideo, Apis.class);
    }

    private RetrofitHelper() { /**/ }

    public static Apis getApis() {
        if (mApiService != null) {
            return mApiService;
        } else {
            throw new IllegalArgumentException("mApiService is null");
        }
    }

    public static Apis getApisHotel() {
        if (mApiHotel != null) {
            return mApiHotel;
        } else {
            throw new IllegalArgumentException("mApiService is null");
        }
    }

    public static Apis getLineApi() {
        if (mApiLine != null) {
            return mApiLine;
        } else {
            throw new IllegalArgumentException("mApiLine is null");
        }
    }

    private static Apis initApiService(String baseUrl, Class clz) {
        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(mOkHttpClient)
            .validateEagerly(true)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

        return (Apis) retrofit.create(clz);
    }
}
